home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10929 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Input filename for fopen()?
  5. Date: Wed, 20 Mar 96 17:43:44 GMT
  6. Organization: none
  7. Message-ID: <827343824snz@genesis.demon.co.uk>
  8. References: <4iikju$nk8@lantana.singnet.com.sg>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4iikju$nk8@lantana.singnet.com.sg>
  15.            s8700055@singnet.com.sg "XY Xie" writes:
  16.  
  17. >How do I get the user to input filename for fopen()  ?
  18. >
  19. > I tried a gets(filename) before a fopen(filename,"r"). It worked for
  20. >files in the current dir (when the user only has to enter the
  21. >filename) , but  it does not work for files in other dirs (when the
  22. >user has to enter something like "c:\\windows\\letter.txt").
  23.  
  24. Did you try having the user enter:
  25.  
  26.   c:\windows\letter.txt
  27.  
  28. or
  29.  
  30.   c:/windows/letter.txt  ?
  31.  
  32. >When I tried to type the filename directly into fopen(), it worked
  33. >fine for any file any where.
  34. >So I guess the problem lies in the ' \ '  because C treats it as an
  35. >escape sequence. How do I get around this?
  36.  
  37. \ is an 'escape' characters in string literals in C source but that doesn't
  38. imply it has any special significance on file input/output. So unless your
  39. OS treats it specially you enter \ for \ and not \\ at runtime.
  40.  
  41. Also gets() is dangerous and should never be used because you can't prevent
  42. it writing beyond the buffer you give it. Use fgets() instead but be sure
  43. you remove any trailing '\n' character before passing the string to fopen().
  44.  
  45. -- 
  46. -----------------------------------------
  47. Lawrence Kirby | fred@genesis.demon.co.uk
  48. Wilts, England | 70734.126@compuserve.com
  49. -----------------------------------------
  50.